﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WindowsForms.AOnline.Presenter.Dictionary.Account;
using WindowsForms.AOnline.Presenter.Dictionary.AutoBusiness;
using WindowsForms.AOnline.Presenter.Dictionary.RefType;
using WindowsForms.AOnline.Presenter.Dictionary.VoucherType;
using WindowsForms.AOnline.View.Dictionary;
using WindowsForms.AOnline.WindowsForm.FormBase;
using WindowsForms.AOnline.WindowsForm.FormBase.PropertyGrid;
using WindowsForms.AOnline.WindowsForm.Resources;

namespace WindowsForms.AOnline.WindowsForm.FormDictionary
{
    public partial class FrmXtraAutoBusinessDetail : FrmXtraBaseCategoryDetail, IAutoBusinessView, IAccountsView, IVoucherTypesView, IRefTypesView
    {
        private readonly AutoBusinessPresenter _autoBusinessPresenter;
        private readonly AccountsPresenter _accountsPresenter;
       // private readonly BudgetSourcesPresenter _budgetSourcesPresenter;
      //  private readonly BudgetItemsPresenter _budgetItemsPresenter;
        private readonly VoucherTypesPresenter _voucherTypesPresenter;
        private readonly RefTypesPresenter _refTypesPresenter;

        public FrmXtraAutoBusinessDetail()
        {
            InitializeComponent();
            _autoBusinessPresenter = new AutoBusinessPresenter(this);
            _accountsPresenter = new AccountsPresenter(this);
            _voucherTypesPresenter = new VoucherTypesPresenter(this);
            _refTypesPresenter = new RefTypesPresenter(this);
        }
        /// <summary>
        /// Initializes the data.
        /// </summary>
        protected override void InitData()
        {
            _accountsPresenter.DisplayInventoryItem();
            _voucherTypesPresenter.DisplayActive();
            _refTypesPresenter.Display();
          //  _budgetSourcesPresenter.DisplayActive();
          //  _budgetItemsPresenter.DisplayActive();
            if (KeyValue != null) { _autoBusinessPresenter.Display(KeyValue); }
        }

        /// <summary>
        /// Focuses the control.
        /// </summary>
        protected override void InitControls()
        {

        }

        /// <summary>
        /// Valids the data.
        /// </summary>
        /// <returns></returns>
        protected override bool ValidData()
        {
            if (string.IsNullOrEmpty(AutoBusinessCode))
            {
                XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResAutoBusinessCode"),
                                ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtAutoBusinessCode.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(AutoBusinessName))
            {
                XtraMessageBox.Show(ResourceHelper.GetResourceValueByName("ResAutoBusinessName"),
                                ResourceHelper.GetResourceValueByName("ResDetailContent"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtAutoBusinessName.Focus();
                return false;
            }
            return true;
        }

        /// <summary>
        /// Saves the data.
        /// </summary>
        /// <returns></returns>
        protected override string SaveData()
        {
            return _autoBusinessPresenter.Save();
        }

        #region IAutoBusinessView Members

        public long UnitId
        {
            get;
            set;
        }

        public long AutoBusinessId
        {
            get;

            set;

        }

        public string AutoBusinessCode
        {
            get { return txtAutoBusinessCode.Text; }
            set { txtAutoBusinessCode.Text = value; }
        }

        public string AutoBusinessName
        {
            get { return txtAutoBusinessName.Text; }
            set { txtAutoBusinessName.Text = value; }
        }

        public int RefTypeId
        {
            get
            {
                if (ReferenceEquals(grdLockUpRefTypeId.EditValue, "")) return 0;
                return (int)grdLockUpRefTypeId.EditValue;
            }
            set
            {
                grdLockUpRefTypeId.EditValue = value;
            }
        }

        public int VoucherTypeId
        {
            get
            {
                if (ReferenceEquals(grdLockUpVoucherTypeId.EditValue, "")) return 0;
                return (int)grdLockUpVoucherTypeId.EditValue;
            }
            set
            {
                grdLockUpVoucherTypeId.EditValue = value;
            }
        }

        public string DebitAccountNumber
        {
            get
            {
                if (ReferenceEquals(grdLookUpDebitAccountNumber.EditValue, "")) return null;
                return (string)grdLookUpDebitAccountNumber.EditValue;
            }
            set { grdLookUpDebitAccountNumber.EditValue = value; }
        }

        public string CreditAccountNumber
        {
            get
            {
                if (ReferenceEquals(grdLookUpCreditAccountNumber.EditValue, "")) return null;
                return (string)grdLookUpCreditAccountNumber.EditValue;
            }
            set { grdLookUpCreditAccountNumber.EditValue = value; }
        }

        public string Description
        {
            get { return memoDescription.Text; }
            set { memoDescription.Text = value; }
        }

        public bool IsActive
        {
            get { return chkIsActive.Checked; }
            set { chkIsActive.Checked = value; }
        }
        #endregion

        #region Combobox

        public IList<Model.BusinessObjects.Dictionary.RefTypeModel> RefTypes
        {
            set
            {
                grdLockUpRefTypeId.Properties.DataSource = value;
                grdLockUpRefTypeIdView.PopulateColumns(value);
                var gridColumnsCollection = new List<XtraColumn>
                    {
                        new XtraColumn { ColumnName = "RefTypeId", ColumnVisible = false },
                       // new XtraColumn { ColumnName = "FunctionId", ColumnVisible = false },
                        new XtraColumn { ColumnName = "MasterTable", ColumnVisible = false },
                        new XtraColumn { ColumnName = "DetailTable", ColumnVisible = false },

                             new XtraColumn { ColumnName = "Description", ColumnVisible = false },
                        new XtraColumn { ColumnName = "IsActive", ColumnVisible = false },
                       // new XtraColumn { ColumnName = "DefaultDebitAccountCategoryCode", ColumnVisible = false },
                       // new XtraColumn { ColumnName = "DefaultDebitAccountCode", ColumnVisible = false },
                      //  new XtraColumn { ColumnName = "DefaultCreditAccountCategoryCode", ColumnVisible = false },
                      //  new XtraColumn { ColumnName = "DefaultCreditAccountCode", ColumnVisible = false },
                       // new XtraColumn { ColumnName = "DefaultObjectType", ColumnVisible = false },
                      //  new XtraColumn { ColumnName = "IsDefaultSetting", ColumnVisible = false },
                     //   new XtraColumn { ColumnName = "IsPostToGL", ColumnVisible = false },
                        new XtraColumn { ColumnName = "Name", ColumnCaption = "Mã loại chứng từ", ColumnPosition = 1, ColumnVisible = true },
                    };
                foreach (var column in gridColumnsCollection)
                {
                    if (column.ColumnVisible)
                    {
                        grdLockUpRefTypeIdView.Columns[column.ColumnName].Caption = column.ColumnCaption;
                        grdLockUpRefTypeIdView.Columns[column.ColumnName].SortIndex = column.ColumnPosition;
                        grdLockUpRefTypeIdView.Columns[column.ColumnName].Width = column.ColumnWith;
                    }
                    else
                    {
                        grdLockUpRefTypeIdView.Columns[column.ColumnName].Visible = false;
                    }
                }
                grdLockUpRefTypeId.Properties.DisplayMember = "Name";
                grdLockUpRefTypeId.Properties.ValueMember = "RefTypeId";
            }
        }

        public IList<Model.BusinessObjects.Dictionary.VoucherTypeModel> VoucherTypes
        {
            set
            {
                grdLockUpVoucherTypeId.Properties.DataSource = value;
                grdLockUpVoucherTypeIdView.PopulateColumns(value);
                var gridColumnsCollection = new List<XtraColumn>
                    {
                        new XtraColumn { ColumnName = "VoucherTypeId", ColumnVisible = false },
                        new XtraColumn { ColumnName = "IsActive", ColumnVisible = false },
                        new XtraColumn { ColumnName = "VoucherTypeName", ColumnCaption = "Mã tài khoản", ColumnPosition = 1, ColumnVisible = true },
                    };
                foreach (var column in gridColumnsCollection)
                {
                    if (column.ColumnVisible)
                    {
                        grdLockUpVoucherTypeIdView.Columns[column.ColumnName].Caption = column.ColumnCaption;
                        grdLockUpVoucherTypeIdView.Columns[column.ColumnName].SortIndex = column.ColumnPosition;
                        grdLockUpVoucherTypeIdView.Columns[column.ColumnName].Width = column.ColumnWith;
                    }
                    else
                    {
                        grdLockUpVoucherTypeIdView.Columns[column.ColumnName].Visible = false;
                    }
                }
                grdLockUpVoucherTypeId.Properties.DisplayMember = "VoucherTypeName";
                grdLockUpVoucherTypeId.Properties.ValueMember = "VoucherTypeId";
            }
        }

        public IList<Model.BusinessObjects.Dictionary.AccountModel> Accounts
        {
            set
            {
                grdLookUpDebitAccountNumber.Properties.DataSource = value;
                grdLookUpCreditAccountNumber.Properties.DataSource = value;
                grdLookUpDebitAccountNumberView.PopulateColumns(value);
                grdLookUpCreditAccountNumberView.PopulateColumns(value);
                var gridColumnsCollection = new List<XtraColumn>
                    {
                    new XtraColumn { ColumnName = "AccountCode", ColumnCaption = "Mã tài khoản", ColumnPosition = 1, ColumnVisible = true, ColumnWith = 200 },
                    new XtraColumn { ColumnName = "AccountName", ColumnCaption = "Tên tài khoản", ColumnPosition = 2, ColumnVisible = true, ColumnWith = 300 },
                    new XtraColumn { ColumnName = "AccountId",  ColumnVisible = false, },
                    new XtraColumn { ColumnName = "ParentId",  ColumnPosition = 3, ColumnVisible = false },
                    new XtraColumn { ColumnName = "Description", ColumnVisible = false, },
                    new XtraColumn { ColumnName = "IsActive", ColumnVisible = false },
                    new XtraColumn { ColumnName = "Grade", ColumnVisible = false },
                    new XtraColumn { ColumnName = "IsDetail", ColumnVisible = false },
                    new XtraColumn { ColumnName = "UnitId", ColumnVisible = false },
                    new XtraColumn { ColumnName = "IsCustomer", ColumnVisible = false },
                    new XtraColumn { ColumnName = "IsVendor", ColumnVisible = false },
                    new XtraColumn { ColumnName = "IsInventoryItem", ColumnVisible = false },                 
                    new XtraColumn { ColumnName = "IsFixedAsset", ColumnVisible = false },
                    new XtraColumn { ColumnName = "IsTax", ColumnVisible = false },
                    new XtraColumn { ColumnName = "IsSystem", ColumnVisible = false }
            };

                foreach (var column in gridColumnsCollection)
                {
                    if (column.ColumnVisible)
                    {
                        grdLookUpDebitAccountNumberView.Columns[column.ColumnName].Caption = column.ColumnCaption;
                        grdLookUpDebitAccountNumberView.Columns[column.ColumnName].SortIndex = column.ColumnPosition;
                        grdLookUpDebitAccountNumberView.Columns[column.ColumnName].Width = column.ColumnWith;
                        grdLookUpCreditAccountNumberView.Columns[column.ColumnName].Caption = column.ColumnCaption;
                        grdLookUpCreditAccountNumberView.Columns[column.ColumnName].SortIndex = column.ColumnPosition;
                        grdLookUpCreditAccountNumberView.Columns[column.ColumnName].Width = column.ColumnWith;
                    }
                    else
                    {
                        grdLookUpDebitAccountNumberView.Columns[column.ColumnName].Visible = false;
                        grdLookUpCreditAccountNumberView.Columns[column.ColumnName].Visible = false;
                    }
                }
                grdLookUpDebitAccountNumber.Properties.DisplayMember = "AccountCode";
                grdLookUpDebitAccountNumber.Properties.ValueMember = "AccountCode";
                grdLookUpCreditAccountNumber.Properties.DisplayMember = "AccountCode";
                grdLookUpCreditAccountNumber.Properties.ValueMember = "AccountCode";
            }
        }
        #endregion
    }
}